package com.zzptc.lxy.mybook.ui.dashboard;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.core.content.SharedPreferencesCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.zzptc.lxy.mybook.Adapter.BookAdapter;
import com.zzptc.lxy.mybook.Bean.Book;
import com.zzptc.lxy.mybook.Bean.ChooseBook;
import com.zzptc.lxy.mybook.Bean.CollectionItem;
import com.zzptc.lxy.mybook.R;
import com.zzptc.lxy.mybook.ReadActivity;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class DashboardFragment extends Fragment {
    private ListView chooselv;
    BookAdapter adapter;
    List<Book> bookList = new ArrayList<>();
    List<ChooseBook> chooseBookList = new ArrayList<>();

//    List<CollectionItem> collectionItemList = new ArrayList<>();

//收藏页面
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        initData();
        initView(view);
        setListener();
        return view;
    }

    private void setListener() {

//        长按删除收藏
        chooselv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                int cid = chooseBookList.get(position).getId();
                LitePal.delete(ChooseBook.class, cid);
                chooseBookList.remove(position);
                bookList.remove(position);
                adapter.notifyDataSetChanged();
                //弹窗
                showToast("取消收藏");
                return false;
            }
        });
        //        点击阅读   跳转到阅读页面
//        chooselv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent it = new Intent(getContext(), ReadActivity.class);
//                it.putExtra("bookimg", collectionItemList.get(position).getBookimg());
//                it.putExtra("bookname", collectionItemList.get(position).getBookname());
//                it.putExtra("authorname", collectionItemList.get(position).getAuthorname());
//                startActivity(it);
//            }
//        });
    }

//    弹窗提醒
    private void showToast(String str) {
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();

    }

    private void initData() {
        SharedPreferences sp = getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        int uid = sp.getInt("userid", -1);
        chooseBookList = LitePal.where("userid=?", uid + "").find(ChooseBook.class);
        for (ChooseBook c : chooseBookList) {
            Book b = LitePal.find(Book.class, c.getBookid());
            bookList.add(b);
        }

    }

    private void initView(View view) {
        chooselv = view.findViewById(R.id.chooselv);
        adapter = new BookAdapter(getContext(), R.layout.book_item, bookList);
        chooselv.setAdapter(adapter);
    }
}